<?php
namespace App\Http\Controllers\ShopWeb;

use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\ShopWeb\PublicBusiness;
use App\Http\Controllers\Business\ShopWeb\Auth\ShopWebAuth;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;

class AdminController extends WebController
{  
    //是否具备创建子账号和查看列表权限
    public function getCreateSubAccountAndListManageAuth() {
        $shopUser = Auth::guard('shopweb')->user();
        $shopUserId = $shopUser->id;
        $shopId = Redis::get('ShopWeb:ShopId:' . $shopUserId);
        if(!$shopId) {
            $shopId= $shopUser->shopid;
        }
        $result = ShopWebAuth::getCreateSubAccountAndListManageAuth($shopUserId, $shopId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
    //权限管理列表
    public function subAccountList() {
        $shopUser = Auth::guard('shopweb')->user();
        $shopUserId = $shopUser->id;
        $shopUserShopIdString = $shopUser->shopid;
        $shopId = Redis::get('ShopWeb:ShopId:' . $shopUserId);
        if(!$shopId) {
            return redirect()->to('/shop/index');
        }
        $shopUserShopIds = explode(',', $shopUserShopIdString);
        $shopUserAuthJson = $shopUser->auth;
        $shopUserAuth = json_decode($shopUserAuthJson, true);
        if(!isset($shopUserAuth[$shopId]) || (!in_array("Admin", $shopUserAuth[$shopId]))) {
            return view('errors.404');
        }
        $result = ShopWebAuth::getAllSubAccountByShopId($shopId, $shopUserId, $shopUserShopIds);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $lists = $result['data'];
        $data = [
            'lists' => $lists
        ];
        return view('shop.admin.account_list', $data);
    }
    //新增帐号时获取验证码
    public function getCodeBeforeAddSubAccount() {
        $mobile = $this->request->get('mobile');
        $shopUser = Auth::guard('shopweb')->user();
        $shopUserId = $shopUser->id;
        $authJson = $shopUser->auth;
        $auth = json_decode($authJson, true);
        $result = ShopWebAuth::getCodeBeforeAddSubAccount($mobile,$auth);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //新增子账号
    public function addSubAccount() {
        $shopUser = Auth::guard('shopweb')->user();
        $shopUserId = $shopUser->id;
        $shopIdString = $shopUser->shopid;
        $shopUserAuthJson = $shopUser->auth;
        $shopUserAuth = json_decode($shopUserAuthJson, true);
        $shopIds = explode(',', $shopIdString);
        $result = ShopWebAuth::getShopDetailsByShopIds($shopIds, $shopUserAuth);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $lists = $result['data'];
        $data = [
            'lists'=>$lists
        ];
        return view('shop.admin.new_account', $data);
    }
    //通过手机号码获取该帐号下的商家id
    public function getShopIdsByMobile() {
        $mobile = $this->request->get('mobile');
        $result = ShopWebAuth::getShopIdsByMobile($mobile);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        return $this->responseSuccessData($result['data']);
    }
    //提交新增帐号
    public function submitAddSubAccount() {
        $adminUser = Auth::guard('shopweb')->user();
        $adminUserId = $adminUser->id;
        $shopIdString = Redis::get('ShopWeb:ShopId:' . $adminUserId);
        if(!$shopIdString) {
            $shopIdString = $adminUser->shopid;
        }
        $shopIds = explode(',', $shopIdString);
        $mobile = $this->request->get('mobile');
        $name = $this->request->get('name');
        $auth = $this->request->get('auth');
        $code = $this->request->get('code');
        $result = ShopWebAuth::addSubAccount($adminUserId, $mobile, $name, $auth, $code, $shopIds);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //获取子账号信息
    public function getSubAccount() {
        $shopUser = Auth::guard('shopweb')->user();
        $shopUserId = $shopUser->id;
        $shopIdString = $shopUser->shopid;
        $shopIds = explode(',', $shopIdString);
        $subAccountId = $this->request->get('subaccountid');
        $result = ShopWebAuth::getSubAccount($shopUserId, $subAccountId, $shopIds);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        return view('shop.admin.check_account', $data);
    }
    //获取子账号的权限
    public function getSubAccountAuth() {
        $shopUserId = Auth::guard('shopweb')->id();
        $subAccountId = $this->request->get('subaccountid');
        $result = ShopWebAuth::getSubAccountAuth($shopUserId, $subAccountId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
    //编辑子账号
    public function editSubAccount() {
        $shopUser = Auth::guard('shopweb')->user();
        $shopUserId = $shopUser->id;
        $shopIdString = $shopUser->shopid;
        $shopIds = explode(',', $shopIdString);
        $subAccountId = $this->request->get('subaccountid');
        $result = ShopWebAuth::getSubAccountBeforeEdit($shopUserId, $subAccountId, $shopIds);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        return view('shop.admin.edit_account', $data);
    }
    //提交新增帐号
    public function submitEditSubAccount() {
        $adminUser = Auth::guard('shopweb')->user();
        $adminUserId = $adminUser->id;
        $subAccountId = $this->request->get('subaccountid');
        $auth = $this->request->get('auth');
        $result = ShopWebAuth::updateSubAccount($adminUserId, $subAccountId, $auth);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //删除子账号
    public function disableSubAccount() {
        $adminUser = Auth::guard('shopweb')->user();
        $adminUserId = $adminUser->id;
        $subAccountId = $this->request->get('subaccountid');
        $shopId = $this->request->get('shopid');
        $result = ShopWebAuth::disableSubAccount($adminUserId, $subAccountId, $shopId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
}